//
//  AddressBook.m
//  OC5_数组课后作业网上答案
//
//  Created by dllo on 15/10/28.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "AddressBook.h"
#import "AddressContact.h"

@implementation AddressBook

- (id)init
{
    self = [super init];
    if (self) {
        _data = [[NSMutableArray alloc] init];
    }
    return self;
}

//2、在main.m中定义可变数组，管理所有联系人。可以添加新联系人对象，如果姓名或电话号码为空，打印添加失败。

- (void)addPerson:(AddressContact *)per
{
    if ([[per name] length] == 0 || [[per phoneNum] length] == 0) {
        NSLog(@"添加失败");
        return;
    }
    [_data addObject:per];
}


//3、获取某个分组下的所有联系人。
- (void)getPersonWithGroupName:(NSString *)groupName
{
    for (AddressContact * p in _data) {
        if ([[p groupName] isEqualToString:groupName]) {
            [p information];
        }
    }
}
//4、根据电话号码搜索联系人。
- (void)getPersonWithphoneNum:(NSString *)phoneNum
{
    for (AddressContact * p in _data) {
        if ([[p phoneNum] isEqualToString:phoneNum]) {
            [p information];
        }
    }
    
}

//5、获取所有女性联系人
- (void)getPersonWithSex:(NSString *)sex
{
    for (AddressContact * p in _data) {
        if ([[p sex] isEqualToString:sex]) {
            [p information];
        }
    }
}
//6、根据姓名删除联系人
- (void)removePerson:(AddressContact *)per
{
    [_data removeObject:per];
}
- (void)removePersonWithName:(NSString *)name
{
    for (AddressContact * p in _data) {
        if ([[p name] isEqualToString:name]) {
            [self removePerson:p];
        }
    }
}
// 7、删除某个分组全部联系人
- (void)removePersonWithGroupName:(NSString *)groupName
{
    NSMutableArray * delete = [NSMutableArray array];
    
    for (AddressContact * p in _data) {
        if ([[p groupName] isEqualToString:groupName]) {
            [delete addObject:p];
        }
    }
    
    [_data removeObjectsInArray:delete];
}

//8、展示通讯录中所有联系人
- (void)showAddressBook
{
    for (AddressContact * per in _data){
        [per information];
    }
}

@end
